package com.fenxiangle.yueding.feature.home.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.CommentBo;
import com.fenxiangle.yueding.entity.bo.CouponBo;
import com.fenxiangle.yueding.entity.bo.PageBo;
import com.fenxiangle.yueding.feature.home.view.adapter.RatingAdapter;
import com.fenxiangle.yueding.framework.api.UserApi;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ViewRatingActivity extends BaseActivity {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_rating)
    RecyclerView mRvRating;
    RatingAdapter ratingAdapter;
    private EmptyView emptyView;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_view_rating;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        initAdapter();
        initToolBar(toolbar, "查看评分", true, true);
    }

    @Override
    protected void initData() {
        String userId = this.getIntent().getStringExtra("uid");
        AM.api().createApiService(UserApi.class)
                .getScore(new PageBo(1, 100, userId))
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<CommentBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(List<CommentBo> commentBo) {
                        dismissLoading();
                        if (commentBo != null && commentBo.size() > 0) {
                            ratingAdapter.setNewData(commentBo);
                        } else {
                            ratingAdapter.setEmptyView(emptyView.getEmptyView());
                            ratingAdapter.setNewData(null);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                        emptyView.getErrorView(e.getMessage());
                        ratingAdapter.setNewData(null);
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }

    private void initAdapter() {
        mRvRating.setLayoutManager(new LinearLayoutManager(this));
        emptyView = new EmptyView(mRvRating);
        ratingAdapter = new RatingAdapter();
        ratingAdapter.bindToRecyclerView(mRvRating);
    }


}
